<template>
	<view class="order">
		<view class="header">
			<navigator open-type="navigateBack" delta="1">
				<image src="../../static/image/fanhui (2).png" mode="widthFix" style="width: 80rpx;"></image>
			</navigator>
			<text>提交订单</text>
		</view>
		<view class="payfor">
			<view class="time">
				<text>剩余支付时间</text>
				<text style="padding-left: 10rpx;color: red;">{{hour}}:</text>
				<text style="color: red;">{{minute}}:</text>
				<text style="color: red;">{{second}}</text>
			</view>
			<view class="price">￥ 475.00</view>
		</view>
		<view class="method">请选择付款方式</view>
		<view class="chooseMethod">
			<view class="item" v-for="item in payForList" :key="item.id" @click="choosePayMethod(item)">
				<view class="icon">
					<image :src="item.src" mode="widthFix"
						style="width: 40rpx;" />
					<text>{{item.content}}</text>
				</view>
				<label class="radio">
					<radio value="" :checked="item.checked" />
				</label>
			</view>
			<view class="">
				{{tip}}
			</view>
			
		</view>
		<view class="button">
			<button @click="payFor">确认支付</button>
		</view>
		<view class="modal">	
			<wyb-modal ref="modal" custom="true" title=" " @confirm="onConfirmClick">
				<view class="success">支付成功</view>
				<view class="watchOrder">
					<navigator url="/pages/home/orderDetail">查看订单</navigator>
				</view>
				<view class="returnHome">
					<navigator open-type="reLaunch" url="/pages/home/home">返回首页</navigator>
				</view>
			</wyb-modal>
		</view>
	</view>
</template>

<script>
	//vuex
	import { createNamespacedHelpers } from "vuex";
	let { mapState, mapActions, mapMutations } = createNamespacedHelpers(
	  "home"
	);
	export default {
		components: {},
		mounted() {},
		data() {
			return {
				payForList:[
					{id:1,content:"微信支付",src:"https://i.loli.net/2021/04/13/JELPdoj614xFAwl.png", checked:false},
					{id:2,content:"支付宝",src:"https://i.loli.net/2021/04/13/BzyIiXjp6GhLJmd.png", checked:false},
					{id:3,content:"钱包",src:"https://i.loli.net/2021/04/13/TIFMcpkjshPVOt1.png", checked:false},
					{id:4,content:"Apple Pay",src:"https://i.loli.net/2021/04/13/nCJhpGsQU9w3bPe.png", checked:false},
				],
				totalTime: 600,
				//倒计时
				timer: null,
				tip:''
			}
		},
		onLoad() {
			this.paytime()
		},
		beforeDestroy() {
			clearInterval(this.timer)
		},
		computed: {
			hour() {
				return Math.floor(this.totalTime / 60 / 60)
			},
			minute() {
				return parseInt(this.totalTime / 60)
			},
			second() {
				return this.totalTime % 60
			},
			
			//支付方式
			payMethod() {
				return this.payForList.filter(item => item.checked == true)
			}
		},
		methods: {
			...mapActions(["carserviceOrder"]),
			//模态框确认
			onConfirmClick() {
				this.$refs.modal.hideModal()
			},
			//剩余支付时间
			paytime() {
				this.timer = setInterval(() => {
					this.totalTime -= 1
				}, 1000)
			},
			//choosePayMethod
			choosePayMethod(item) {
				this.payForList.forEach(item => item.checked = false)
				item.checked = true
			},
			//支付
			async payFor() {
				const token = uni.getStorageSync('token');
				console.log(token);
				//下单
				const shopId = uni.getStorageSync('shopId');
				console.log(shopId);
				if (token) {
					if(this.payMethod[0]) {
						const data = await this.carserviceOrder({
							shopId,
							appointTime:"13:00-15:00",
							couponInfoId:0,
							carserviceId:1
						})
						if(data.code) {
							this.$refs.modal.showModal();
						}
					}else {
						this.tip = '请选择支付方式'
					}
				} else {
					uni.navigateTo({
						url: '/pages/login/login'
					})
				}
				
			}
		},
		watch: {
			totalTime: {
				handler(value, oldVal) {
					if (value < 1) {
						clearInterval(this.timer)
						uni.redirectTo({
							url: "pages/home/home"
						})
					}
				},
				immediate: true
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.order {
		position: relative;
		background-color: #f9f9f9;
		height: 800px;

		.header {
			height: 340rpx;
			background: linear-gradient(120deg, #fd8839, #fd873c, #fd8846, #fd8c38, #fda548, #fda548, #fd793a, #fd883a);
			color: white;
			display: flex;
			align-items: center;
			box-sizing: border-box;
			padding: 40rpx 0;

			text {
				margin-left: 200rpx;
			}
		}

		.payfor {
			width: 660rpx;
			height: 200rpx;
			position: absolute;
			padding: 0 24rpx;
			top: 220rpx;
			left: 0;
			background-color: #FFFFFF;
			margin: 24rpx;
			border-radius: 20rpx;
			display: flex;
			justify-content: center;
			flex-direction: column;
			align-items: center;
			box-shadow: -1px 1px 3px 1px rgba(gray, 0.4);

			.time {
				font-size: 22rpx;
			}

			.price {
				color: #ff5300;
				font-weight: bold;
			}
		}

		.method {
			margin-top: 140rpx;
			font-size: 20rpx;
			color: #999999;
			padding: 24rpx;
		}

		.chooseMethod {
			background-color: #FFFFFF;
			padding: 24rpx;
			margin: 24rpx;
			border-radius: 20rpx;

			.item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin: 10rpx 0;

				.icon {
					text {
						margin: 0 20rpx;
					}
				}

				.radio {
					radio {
						scale: 0.7;
					}
				}
			}
		}

		.button {
			button {
				border-radius: 40rpx;
				background-color: #ff660a;
				width: 80%;
				color: white;
				margin: 120rpx auto;
			}
		}

		.modal {
			text-align: center;

			.success {
				font-size: 40rpx;
				color: #ff5401;
				margin: 20rpx 0;
			}

			.watchOrder {
				font-size: 40rpx;
				color: #FFFFFF;
				background-color: #ff5300;
				border-radius: 40rpx;
				width: 300rpx;
				height: 80rpx;
				line-height: 80rpx;

			}

			.returnHome {
				color: #f26200;
				font-size: 32rpx;
				margin: 20rpx 0;
			}
		}
	}
</style>
